1. Field of the Invention
The present invention relates to a memory management method and system, particularly to garbage collection.
2. Description of Related Art
Some languages with garbage collection as a memory management function, such as Java (registered trademark of U.S., Sun Microsystems Inc.), allow defining a method of performing such a process as freeing up resources in performing garbage collection. The above method is called a finalize method in Java, and is also referred to as “a finalize method” hereinbelow. In garbage collection, if a collectable (unused) object does not have a finalize method, it is readily collected. Such an object is referred to as “a collectable (unreachable) object.” On the other hand, if a collectable object has a finalize method it should not be readily collected. Such a collectable object is referred to as “a finalizable object.” Instead, the finalize method should be executed before the collection. After a thread for performing finalization (finalize process) (such a thread is referred to as “finalizer thread”) executes the finalize method, the finalizable object is collected, typically, in the next execution cycle of garbage collection.
There are several kinds of garbage collection. One typical kind of garbage collection is a technique called copying GC (refer to C. J. Cheney, “A Nonrecursive List Compacting Algorithm,” Communications of the ACM 13 (11), 1970). In this copying GC, an entire heap is partitioned into two or more areas, and new objects are allocated in one of those areas. When no free space is left in the currently used area referred to as “area of use,” garbage collection is activated. Objects reachable from a root set (that is, possibly-used objects) are copied into one of the areas other than the area of use that has been used for new object allocation. Such an area into which objects are copied is referred to as “To area.” After the garbage collection is completed, new objects are allocated in the To area at positions subsequent to the copies of the objects.
As described above, a finalizable object having a finalize method is collected after the finalize method is executed. Here, to execute the finalize method, collection of all the objects reachable from the finalizable object (finalizer-reachable objects) must be delayed till the finalize method is completed.
In implementation of the garbage collection, the following procedure is performed:
1. search objects reachable from a root set (stack or the like);
2. add finalizable objects determined from a result of the above search to a queue (finalize queue) waiting for finalization of a finalizer thread;
3. search again for objects reachable from the finalizable objects; and
4. collect unnecessary objects.
In a workload generating a large number of objects each having a finalize method, the number of finalizable objects and objects reachable from the finalizable objects is large enough to reduce the collection rate in a memory. This increases not only the frequency of garbage collection but also the execution time of the garbage collection.
A copying garbage collector cannot readily collect finalizable objects and finalizer-reachable objects, even if the objects are unreachable from a root set, because their finalize methods should be executed before the collection. Hence the certain objects should be copied to the To area for the present. This means that objects other than objects reachable from the root set will be copied, and thus leads to decrease in efficiency.